#include <bits/stdc++.h>
#define SINGLE_INPUT
#define ll long long
#define ull unsigned long long
#define N 500005
#define MOD 998244353
#define INF 0x3f3f3f3f
using namespace std;
// void sol() {
// int n, s, k;
// cin >> n >> s >> k;
// s--;
// vector<int> r(n);
// for (auto& i : r)
// cin >> i;
// string c;
// cin >> c;
// vector<vector<int>> f(n, vector<int>(k + 51, INF));
// for (int i = 0; i < n; i++) {
// f[i][r[i]] = abs(i - s);
// }
// for (int j = 0; j <= k; j++) {
// for (int i = 0; i < n; i++) {
// for (int z = 0; z < n; z++) {
// if (c[i] == c[z] || r[i] >= r[z])
// continue;
// f[z][j + r[z]] = min(f[z][j + r[z]], f[i][j] + abs(i - z));
// }
// }
// }
// int ans = INF;
// for (int i = 0; i < n; i++) {
// for (int j = 0; j <= 50; j++) {
// ans = min(ans, f[i][j + k]);
// }
// }
// // for (int i = 0; i < n; i++) {
// // for (int j = 0; j <= k; j++) {
// // cout << i << " " << j << " " << f[i][j] << "\n";
// // }
// // }
// if (ans == INF)
// cout << "-1\n";
// else
// cout << ans << "\n";
// }
void sol() {
int n, s, k;
cin >> n >> s >> k;
s--;
vector<int> r(n);
for (auto& i : r)
cin >> i;
string c;
cin >> c;
vector<vector<int>> f(n, vector<int>(k + 1, INF * 2));
// 在x位置还有y个要收集,所需的最小时间
function<int(int, int)> dfs = [&](int x, int y) {
if (r[x] >= y) {
return f[x][y] = min(abs(x - s), f[x][y]);
}
if (f[x][y] != INF * 2)
return f[x][y];
f[x][y] = INF;
for (int i = 0; i < n; i++) {
if (c[x] != c[i] && r[i] < r[x])
f[x][y] = min(f[x][y], dfs(i, y - r[x]) + abs(i - x));
}
return f[x][y];
};
int ans = INF;
for (int i = 0; i < n; i++) {
ans = min(ans, dfs(i, k));
}
if (ans == INF) {
cout << "-1\n";
} else {
cout << ans << "\n";
}
}
int main() {
cout << setprecision(15) << fixed;
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef SINGLE_INPUT
int t;
cin >> t;
while (t--) {
sol();
}
#else
sol();
#endif
return 0;
}
468. Validate IP Address | 450. Delete Node in a BST |
445. Add Two Numbers II | 442. Find All Duplicates in an Array |
437. Path Sum III | 436. Find Right Interval |
435. Non-overlapping Intervals | 406. Queue Reconstruction by Height |
380. Insert Delete GetRandom O(1) | 332. Reconstruct Itinerary |
368. Largest Divisible Subset | 377. Combination Sum IV |
322. Coin Change | 307. Range Sum Query - Mutable |
287. Find the Duplicate Number | 279. Perfect Squares |
275. H-Index II | 274. H-Index |
260. Single Number III | 240. Search a 2D Matrix II |
238. Product of Array Except Self | 229. Majority Element II |
222. Count Complete Tree Nodes | 215. Kth Largest Element in an Array |
198. House Robber | 153. Find Minimum in Rotated Sorted Array |
150. Evaluate Reverse Polish Notation | 144. Binary Tree Preorder Traversal |
137. Single Number II | 130. Surrounded Regions |